// pages/taskList/taskList.js
var url = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioCheckVal: 0,
    baseUrl: '',
    id:'',
    service: {},
    num: 1,
    isShow: false,
    taskBaominglist:[]
  },
  payLook: function () {
    if (this.data.taskBaominglist.length <= 5) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    this.setData({
      isShow: !this.data.isShow
    })
  },
  del: function () {
    if (this.data.num > 0) {
      this.setData({
        num: this.data.num - 1
      })
    }
  },
  add: function () {
    this.setData({
      num: this.data.num + 1
    })
  },
  gotobuy: function () {
    if (this.data.num == 0) {
      wx.showToast({
        title: '数量不能为0',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/buy', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'POST',
      data: {
        "good_id": this.data.service.good_id,
        "num": this.data.num,
        "job_id": this.data.id
      },
      success: (res) => {
        console.log(res, '服务订单成功')
        wx.request({
          url: 'https://api.tianlala.com.cn/order', // 仅为示例，并非真实的接口地址
          header: {
            'content-type': 'application/json', // 默认值
            'Authorization': accesstoken
          },
          method: 'POST',
          data: {
            "out_trade_no": res.data.out_trade_no
          },
          success: (res) => {
            console.log(res, '成功');
            wx.requestPayment({
              timeStamp: res.data.result.timeStamp,
              nonceStr: res.data.result.nonceStr,
              package: res.data.result.package,
              signType: res.data.result.signType,
              paySign: res.data.result.paySign,
              success: (res) => {
                console.log(res, "支付成功");
                this.setData({
                  isShow: false
                })
                wx.showToast({
                  title: '开通成功',
                })
                this.getTaskBaomingList(this.data.id);
              },
              fail(res) {
                console.log(res, "支付失败");
              }
            })

          },
          fail: (res) => {
            console.log(res, '失败')
          }
        })

      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   radioCheckVal: options.id
    // })
    this.setData({
      baseUrl: url.baseUrl,
      id:options.id
    })
    this.getTaskBaomingList(options.id);
    this.getServices(8);
  },
  getServices: function (id) {
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/services', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '服务成功')
        var services = res.data;
        wx.hideLoading();
        for (var i = 0; i < services.length; i++) {
          var service = services[i];
          if (service.good_id == id) {
            this.setData({
              service: service
            })
          }
        }

      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  //报名列表
  getTaskBaomingList: function (id) {
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/missions/posts/' + id + '/users',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '我的任务报名列表')
        this.setData({
          taskBaominglist: res.data
        })
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  radioCheckedChange: function (e) {
    this.setData({
      radioCheckVal: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})